package com.cs.sso.controller;

import com.cs.sso.exception.LoginException;
import com.cs.sso.service.LoginService;
import com.cs.sso.vo.LoginToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cs.common.vo.ReturnVo;

@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("go")
    public ReturnVo go(@RequestParam("username") String username,
                       @RequestParam("password") String password) {

        ReturnVo returnVo = new ReturnVo();
        LoginToken loginToken;
        try {
            loginToken = loginService.login(username, password);
            returnVo.success("", loginToken);
        } catch (LoginException le) {
            returnVo.failure(le.getMessage());
        }

        return returnVo;
    }
}
